#ifndef CONDITION
#define CONDITION
#include <iostream>
#include <map>
#include "../../base/TemplateParser/Parser.h"
using namespace std;
class Condition:public Parser
{
	public:
		bool parseCondition(string);
		bool isCondition(string);
	private:
		bool compare(string);

 };

bool Condition::isCondition(string dir){
	return false;
}

bool Condition::parseCondition(string in){
	bool rt=false;
	string::iterator it = in.begin();
	if(*it=='!')return !parseCondition(in);
	BO::parseitem cd;

	//remove ()
	in=removeCurlyBracket(in);

	//cout<<in<<endl;
	//search || &&
	string::size_type pos=0;
	//position of first || or &&
	string::size_type firstpos=string::npos;
	int open_bracket_count=0;
	it = in.begin();

	while(it!=in.end()){
		//check if is in bracket
		if(*it=='(')open_bracket_count++;
		else if(*it==')')open_bracket_count--;
		//if | is not belong to any bracket then get pos
		else if(*it=='|' && *(it+1)=='|' && open_bracket_count==0){firstpos=pos;cd.direction="||";break;}
		else if(*it=='&' && *(it+1)=='&' && open_bracket_count==0 && firstpos==string::npos){firstpos=pos;cd.direction="&&";}
		it++;
		pos++;
	}
	if(firstpos!=string::npos){
		cd.out=in.substr(0,firstpos);
		firstpos+=2;
		cd.in=in.substr(firstpos,in.size()-firstpos);
		//if(in=="test<\"f\"&&\"e\"==test2")cout<<compare.in<<"aaa"<<endl;
	}
	//if no found then compare !=, ==, <=, >=, <, >
	else{

		return compare(in);
	}

	if(cd.direction=="||")return parseCondition(cd.out)||parseCondition(cd.in);
	if(cd.direction=="&&")return parseCondition(cd.out)&&parseCondition(cd.in);

	return rt;
}
//compare
bool Condition::compare(string in){
	//check !(...)
	bool iscomparenum=false;
	//remove ()
	in=removeCurlyBracket(in);
	bool isleftnum=false;
	bool isrightnum=false;
	BO::parseitem cd;

	//check number
	string::size_type pos=0;
	//position of first || or &&
	string::iterator it = in.begin();

	while(it!=in.end()){
		if(*it=='>' && *(it+1)=='='){cd.direction=">=";break;}
		else if(*it=='<' && *(it+1)=='='){cd.direction="<=";break;}
		else if(*it=='!' && *(it+1)=='='){cd.direction="!=";break;}
		else if(*it=='=' && *(it+1)=='='){cd.direction="==";break;}
		else if(*it=='>'){cd.direction=">";break;}
		else if(*it=='<'){cd.direction="<";break;}
		it++;
		pos++;
	}
	if(it!=in.end()){
		cd.out=parseOperation(in.substr(0,pos));
		pos=pos+cd.direction.size();
		cd.in=parseOperation(in.substr(pos,in.size()-pos));
		//if(in=="test<\"f\"&&\"e\"==test2")cout<<compare.in<<"aaa"<<endl;
	}
	else{
		if(in=="true")return true;
		else if(in=="false")return false;
		//if parse operator
		string t=parseOperation("bool("+in+")");
		if(t=="true")return true;
		else return false;
	}

	//check is number
	if(isstring(cd.out))cd.out=removequote(cd.out);
	else isleftnum=StringProcessor::isnum(cd.out);
	if(isstring(cd.in))cd.in=removequote(cd.in);
	else isrightnum=StringProcessor::isnum(cd.in);

	//cout<<cd.out<<isleftnum<<cd.in<<isrightnum<<endl;
	//cout<<cd.out<<cd.direction<<cd.in<<endl;
	//if both is number then set mode compare num is true
	if(isleftnum && isrightnum)iscomparenum=true;
	//else if 1 of 2 is number, then return false, bc we can compare a string and a number
	else if(isleftnum || isrightnum)return false;
	//else both is string
	//cout<<cd.out<<cd.direction<<cd.in<<endl;
	//compare
	if(cd.direction=="=="){
		if(iscomparenum)return StringProcessor::atoi(cd.out)==StringProcessor::atoi(cd.in);
		else return cd.out==cd.in;
	}
	else if(cd.direction=="!="){
		if(iscomparenum)return StringProcessor::atoi(cd.out)!=StringProcessor::atoi(cd.in);
		else return cd.out!=cd.in;
	}
	else if(cd.direction==">="){
		if(iscomparenum)return StringProcessor::atoi(cd.out)>=StringProcessor::atoi(cd.in);
		else return cd.out>=cd.in;
	}
	else if(cd.direction=="<="){
		if(iscomparenum)return StringProcessor::atoi(cd.out)<=StringProcessor::atoi(cd.in);
		else return cd.out<=cd.in;
	}
	else if(cd.direction==">"){
		if(iscomparenum)return StringProcessor::atoi(cd.out)>StringProcessor::atoi(cd.in);
		else return cd.out>cd.in;
	}
	else if(cd.direction=="<"){
		if(iscomparenum)return StringProcessor::atoi(cd.out)<StringProcessor::atoi(cd.in);
		else return cd.out<cd.in;
	}
	return false;
}

#endif
